﻿namespace UsageTracker.Server
{
    using System;
    using System.Net.Http;
    using System.Reflection;
    using System.Web.Http;

    using log4net;

    using Newtonsoft.Json.Linq;

    using UsageTracker.MongoDb.Processor;

    public class UsageDataTelemetryController : ApiController
    {
        //Initialize the logger
        private readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// Method to accept usage data POSTed in the HTTP form body
        /// </summary>
        /// <param name="usageData"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage PostUsageData([FromBody] string usageData)
        {
            HttpResponseMessage responseMsg = null;
            try
            {
                if (usageData != null)
                {
                    JObject jObj = JObject.Parse(usageData);

                    if (jObj["ProductName"] != null)
                    {
                        DbProcessor mongoDb = new DbProcessor(jObj["ProductName"].ToString());
                        mongoDb.Insert(usageData);
                        responseMsg = this.Request.CreateResponse(System.Net.HttpStatusCode.OK);
                    }
                    else
                    {
                        this.log.Error("Usage data received does not contain a product name.");
                        responseMsg = this.Request.CreateResponse(System.Net.HttpStatusCode.BadRequest);
                    }
                }
                else
                {
                    responseMsg = this.Request.CreateResponse(System.Net.HttpStatusCode.NoContent);
                }
            }
            catch (Exception exp)
            {
                this.log.Error("Could not process the incoming usage data due to an error - " + exp.Message + Environment.NewLine + exp.StackTrace);
                responseMsg = this.Request.CreateResponse(System.Net.HttpStatusCode.BadRequest);
            }
            return responseMsg;
        }


        /// <summary>
        /// Method to accept usage data POSTed in the HTTP request querystring
        /// </summary>
        /// <param name="usageData"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage PostUsageDataOnQueryString(string usageData)
        {
            HttpResponseMessage responseMsg = null;
            try
            {
                if (usageData != null)
                {
                    JObject jObj = new JObject(usageData);
                    if (jObj["ProductName"] != null)
                    {
                        DbProcessor mongoDb = new DbProcessor(jObj["ProductName"].ToString());
                        mongoDb.Insert(usageData);
                        responseMsg = this.Request.CreateResponse(System.Net.HttpStatusCode.OK);
                    }
                    else
                    {
                        this.log.Error("Usage data received does not contain a product name.");
                        responseMsg = this.Request.CreateResponse(System.Net.HttpStatusCode.BadRequest);
                    }
                }
                else
                {
                    responseMsg = this.Request.CreateResponse(System.Net.HttpStatusCode.NoContent);
                }
            }
            catch (Exception exp)
            {
                this.log.Error(
                    "Could not process the incoming usage data due to an error - " + exp.Message
                    + Environment.NewLine + exp.StackTrace);
                responseMsg = this.Request.CreateResponse(System.Net.HttpStatusCode.BadRequest);
            }
            return responseMsg;
        }
    }
}
